import { _decorator, AudioClip, Component, SpriteFrame } from "cc";
import { ResourceConst } from "../const/ResourceConst";
import { Prefab } from "cc";

const { ccclass, property } = _decorator;

@ccclass('ResourceManager')
export class ResourceManager extends Component {
    @property({ type: [ResourceConst.TextureData] })
    public textureData: ResourceConst.TextureData[] = [];

    @property({ type: [ResourceConst.PrefabData] })
    public prefabDatas: ResourceConst.PrefabData[] = [];

    @property({ type: [ResourceConst.AudioData] })
    public audioDatas: ResourceConst.AudioData[] = [];

    private static _instance: ResourceManager;

    public static get I(): ResourceManager {
        return ResourceManager._instance;
    }

    protected onLoad(): void {
        ResourceManager._instance = this;
    }

    public getSpriteFrame(key: string): SpriteFrame {
        const textureData = this.textureData.find((item) => item.key === key);
        return textureData ? textureData.spriteFrame : null;
    }

    public getPrefab(key: string): Prefab {
        const prefabData = this.prefabDatas.find((item) => item.key === key);
        return prefabData ? prefabData.prefab : null;
    }

    public getAudioClip(key: string): AudioClip {
        const audioData = this.audioDatas.find((item) => item.key === key);
        return audioData ? audioData.audioClip : null;
    }
}

